//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.app.service;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class CompressUtils {
    public CompressUtils() {
    }

    public static void main(String[] args) throws Exception {
        String filePath = "直播试题导入样例/台芒/商品图片/详情图/干扰图/080f5c5df52c4ae197a8fd6a7ce33268.png";
        System.out.println();
    }

    public static void compress(String filePath) throws Exception {
        String ext = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();
        if (".zip".equals(ext)) {
            try {
                unpack(filePath);
            } catch (Exception var3) {
                var3.printStackTrace();
                throw new Exception("解压失败");
            }
        } else {
            throw new Exception("暂只支持zip压缩文件，请压缩成zip格式文件");
        }
    }

    public static void unpack(String filepath) throws Exception {
        String sourceFileNameExt = filepath.substring(filepath.lastIndexOf("\\"));
        String sourceFileName = sourceFileNameExt.substring(0, sourceFileNameExt.lastIndexOf("."));
        File zipFile = new File(filepath);
        String desPath = filepath.substring(0, filepath.lastIndexOf("\\")) + sourceFileName + "\\";
        File descDir = new File(desPath);

        try {
            ZipArchiveInputStream inputStream = getZipFile(zipFile);
            Throwable var7 = null;

            try {
                if (!descDir.exists()) {
                    descDir.mkdirs();
                }

                ZipArchiveEntry entry = null;

                while((entry = inputStream.getNextZipEntry()) != null) {
                    if (entry.isDirectory()) {
                        File directory = new File(descDir, entry.getName());
                        directory.mkdirs();
                    } else {
                        BufferedOutputStream os = null;

                        try {
                            String entryPath = entry.getName();
                            File parentFile = new File(descDir + "/" + entryPath.substring(0, entryPath.lastIndexOf("/") + 1));
                            if (!parentFile.exists()) {
                                parentFile.mkdirs();
                            }

                            os = new BufferedOutputStream(new FileOutputStream(new File(descDir, entry.getName())));
                            IOUtils.copy(inputStream, os);
                        } finally {
                            IOUtils.closeQuietly(os);
                        }
                    }
                }
            } catch (Throwable var28) {
                var7 = var28;
                throw var28;
            } finally {
                if (inputStream != null) {
                    if (var7 != null) {
                        try {
                            inputStream.close();
                        } catch (Throwable var26) {
                            var7.addSuppressed(var26);
                        }
                    } else {
                        inputStream.close();
                    }
                }

            }

        } catch (Exception var30) {
            var30.printStackTrace();
            throw var30;
        }
    }

    private static ZipArchiveInputStream getZipFile(File zipFile) throws Exception {
        return new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
    }

    public static void dozip(String sourceFilePath, String zipFilePath, String fileName) {
        boolean flag = false;
        File sourceFile = new File(sourceFilePath);
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        ZipOutputStream zos = null;
        if (!sourceFile.exists()) {
            System.out.println("待压缩的文件目录：" + sourceFilePath + "不存在.");
        } else {
            try {
                File zipFile = new File(zipFilePath + "/" + fileName + ".zip");
                if (zipFile.exists()) {
                    System.out.println(zipFilePath + "目录下存在名字为:" + fileName + ".zip打包文件.");
                } else {
                    File[] sourceFiles = sourceFile.listFiles();
                    if (null != sourceFiles && sourceFiles.length >= 1) {
                        fos = new FileOutputStream(zipFile);
                        zos = new ZipOutputStream(new BufferedOutputStream(fos));
                        byte[] bufs = new byte[10240];

                        for(int i = 0; i < sourceFiles.length; ++i) {
                            ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
                            zos.putNextEntry(zipEntry);
                            fis = new FileInputStream(sourceFiles[i]);
                            bis = new BufferedInputStream(fis, 10240);
                            boolean var14 = false;

                            int read;
                            while((read = bis.read(bufs, 0, 10240)) != -1) {
                                zos.write(bufs, 0, read);
                            }
                        }

                        flag = true;
                    } else {
                        System.out.println("待压缩的文件目录：" + sourceFilePath + "里面不存在文件，无需压缩.");
                    }
                }
            } catch (FileNotFoundException var23) {
                var23.printStackTrace();
                throw new RuntimeException(var23);
            } catch (IOException var24) {
                var24.printStackTrace();
                throw new RuntimeException(var24);
            } finally {
                try {
                    if (null != bis) {
                        bis.close();
                    }

                    if (null != zos) {
                        zos.close();
                    }
                } catch (IOException var22) {
                    var22.printStackTrace();
                    throw new RuntimeException(var22);
                }

            }
        }

    }

    public static void compressFolder(String sourceFilePath, String destFileName) {
        File f = new File(sourceFilePath);
        File out = new File(destFileName);
        if (f != null) {
            Map<String, File> map = new HashMap();
            list(f, (String)null, map);
            if (!map.isEmpty()) {
                try {
                    ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(out);
                    Iterator var6 = map.entrySet().iterator();

                    while(var6.hasNext()) {
                        Entry<String, File> entry = (Entry)var6.next();
                        File file = (File)entry.getValue();
                        ZipArchiveEntry zae = new ZipArchiveEntry(file, (String)entry.getKey());
                        zaos.putArchiveEntry(zae);
                        InputStream is = new FileInputStream(file);
                        byte[] b = new byte[5120];
                        boolean var12 = true;

                        int i;
                        while((i = is.read(b)) != -1) {
                            zaos.write(b, 0, i);
                        }

                        is.close();
                        zaos.closeArchiveEntry();
                    }

                    zaos.finish();
                    zaos.close();
                } catch (IOException var13) {
                    ;
                }
            }
        }

    }

    private static void list(File f, String parent, Map<String, File> map) {
        String name = f.getName();
        if (parent != null) {
            name = parent + "/" + name;
        }

        if (f.isFile()) {
            map.put(name, f);
        } else if (f.isDirectory()) {
            File[] var4 = f.listFiles();
            int var5 = var4.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                File file = var4[var6];
                list(file, name, map);
            }
        }

    }
}
